const express = require('express')

const app = express()

// ----------路由路径----------

// http://localhost:3000/
app.get('/', (req, res) => {
  res.send('Hello World!')
})

// http://localhost:3000/random.text
app.get('/random.text', function (req, res) {
  res.send('random.text')
})

// http://localhost:3000/122213121fly
// 以fly结尾
app.get(/.*fly$/, function (req, res) {
  res.send('/.*fly$/')
})

// http://localhost:3000/abe
// http://localhost:3000/abcde
app.get('/ab(cd)?e', function (req, res) {
  res.send('ab(cd)?e')
})

// http://localhost:3000/acd
// http://localhost:3000/abcd
app.get('/ab?cd', function (req, res) {
  res.send('ab?cd')
})

// http://localhost:3000/ab112233cd
// 这条路线的路径将会匹配abcd，abxcd，abRANDOMcd，ab123cd，等。
app.get('/ab*cd', function (req, res) {
  res.send('ab*cd')
})

// http://localhost:3000/41afe
// 路径包含a
app.get(/a/, function (req, res) {
  res.send('/a/')
})

// ----------路径参数----------

// http://localhost:3000/users/1
// userId 只能输入数字
app.get('/users/:userId(\\d+)', (req, res) => {
  res.send('/users/:userId(\\d+)' + req.params.userId)
})

// http://localhost:3000/users/a
app.get('/users/:id', (req, res) => {
  console.log()
  res.send('get /users/' + req.params.id)
})

// http://localhost:3000/users/111/edit/222/abc
app.get('/users/:id1/edit/:id2/abc', (req, res) => {
  console.log()
  res.send(`get /users/${req.params.id1}/edit/${req.params.id2}/abc`)
})

app.listen(3000, () => {
  console.log(`Server running at http://localhost:3000/`)
})
